function person_dossier() {
    var _this = this;
    var page;
    var companyIds = [];
    var deptIds = [];
    var comPIds = [];
    var departmentNames = [];
    var rootType = '';


    _this.init = function (page_person) {
        page = page_person;
        $("#recursion").attr("checked",false);
        groupTree.init({"ifShow":"false"});
        _this.addDefaultNode();
        var rootNode = getRoots()[0];
        rootType = rootNode.type;
        _this.getAllchildrenNodes(comPIds, departmentNames, rootNode, rootType);
        window.groupTree_search = _this.search;
        _this.search();
        // _this.initListTable();

        $('#click_role_type').on("click", "span", function () {
            $('#click_role_type').find("span").removeClass("active");
            $(this).addClass("active");
            _this.bind_click(this);
        });

        //全选与非全选
        /*        $("#checkAll").click(function () {
                    if (this.checked) {
                        $("#person_table :checkbox").prop("checked", true);
                    } else {
                        $("#person_table :checkbox").prop("checked", false);
                    }

                    _this.getCheckedRows_ids();
                });*/
    };

    _this.initListTable = function () {
        var companyId = $("#companyId").val();
        $("#companyIds").val("");
        $("#deptIds").val(companyId);
        _this.initTable();
    };

    _this.initTable = function () {
        var list_url = appPath + "/admin/dossier/person/list";
        var project_url = appPath + "/admin/dossier/person/project?user_id=";
        var project_off_url = appPath + "/admin/dossier/person/project_off?user_id=";
        var project_center_url = appPath + "/admin/dossier/person/projectCenter?user_id=";
        var study_self_url = appPath + "/admin/dossier/person/studySelf?user_id=";
        page.init("person_form", list_url, "person_table", "person_page", 1, 10);
        page.goPage(1);

        page.list = function (dataList) {
            var len = dataList.length;
            var inner = "", item;
            // 组装数据

            for (var i = 0; i < len; i++) {
                item = dataList[i];
                inner += '<tr>';
                inner += '<td width="40">' + (i + 1) + '</td>';
                inner += "<td width='30'><input type='checkbox' id='person_" + i + "' name='checkPerson' value='" + item['user_id'] + "'/></td>";
                inner += '<td width="120">';
                inner += '<span class="text-orange">' + item['user_name'] + '</span>';
                inner += '</td>';
                inner += '<td width="80">'+ item['role_name'] + '</td>';
                var companyName = item.company_name;
                if (companyName == '' || companyName == undefined || companyName == null) {
                    inner += "<td width='140'>" + companyName + "</td>";
                } else {
                    inner += '<td width="150">' + companyName + '</td>';
                }

                var dept_name = item.dept_name;
                if (dept_name == '' || dept_name == undefined || dept_name == null) {
                    inner += "<td width='107'>/</td>";
                } else {
                    inner += "<td width='107'><span class=\"text-orange\">" + dept_name + "</span></td>";
                }
                inner += '<td width="80">' + item['requirement_year_studytime'] + '</td>';
                inner += '<td width="80">' + TimeUtil.getHouAndMinAndSec(item['year_studytime']) + '</td>';
                inner += '<td width="80">' + TimeUtil.getHouAndMinAndSec(item['total_studytime']) + '</td>';
                if($("#toolboxSync").length > 0){
                    inner += '<td width="80">' + TimeUtil.getHouAndMinAndSec(item['toolboxRec'])+ '</td>';
                }
                inner += '<td width="270">';
                var permissionList =$("#permissionList").val();

                inner += '<a href="' + project_url + item['user_id'] + '" target="_blank" class="a a-info">项目档案</a>';
                /*if (permissionList.indexOf('admin:offProject')==-1 && permissionList.indexOf('admin:centerProject') ==-1) {
                    inner += '<a href="' + project_url + item['user_id'] + '" target="_blank" class="a a-info">项目记录</a>'
                }
                if (permissionList.indexOf('admin:offProject')!=-1 && permissionList.indexOf('admin:centerProject') !=-1) {
                    inner += '<a href="' + project_url + item['user_id'] + '" target="_blank" class="a a-info">线上项目记录</a>'
                    inner += '<a href="' + project_off_url + item['user_id'] + '" target="_blank" class="a a-info">线下项目记录</a>'
                    inner += '<a href="' + project_center_url + item['user_id'] + '" target="_blank" class="a a-info">集中培训记录</a>'
                }
                if (permissionList.indexOf('admin:offProject')!= -1 && permissionList.indexOf('admin:centerProject') ==-1) {
                    inner += '<a href="' + project_url + item['user_id'] + '" target="_blank" class="a a-info">线上项目记录</a>'
                    inner += '<a href="' + project_off_url + item['user_id'] + '" target="_blank" class="a a-info">线下项目记录</a>'
                }
                if (permissionList.indexOf('admin:centerProject') !=-1 && permissionList.indexOf('admin:offProject')==-1) {
                    inner += '<a href="' + project_url + item['user_id'] + '" target="_blank" class="a a-info">线上项目记录</a>'
                    inner += '<a href="' + project_center_url + item['user_id'] + '" target="_blank" class="a a-info">集中培训记录</a>'
                }*/
                inner += '<a href="' + study_self_url + item['user_id'] + '" class="a a-info" target="_blank">自学档案</a>'
                inner += '<button  class="a a-info" onclick="exportInfo(\'' + item['user_id'] + '\')"><i class="fa">导出</i></button>'
                inner += '</td>';
                inner += '</tr>';
            }
            return inner;
        }


        //单个导出
        exportInfo = function (userId) {
            window.open(appPath + '/admin/dossier/person/exportInfo?userId=' + userId);
        }


    };





    function getAllParentNodes(deptId) {
        var contactName = '';
        var treeObj = $.fn.zTree.getZTreeObj(tree_options.id);
        var treeNode = treeObj.getNodeByParam("id", deptId, null);
        if (treeNode == null) {
            return "";
        } else if (treeNode.type == "1") {
            return "";
        } else {
            contactName = contactName == '' ? treeNode.name : treeNode.name + '/' + contactName;
            if (treeNode.getParentNode()) {
                var vl = getAllParentNodes(treeNode.getParentNode().id);
                if (vl != '') {
                    contactName = contactName == '' ? vl : vl + '/' + contactName;
                }
            }
        }
        return contactName;
    }

    //搜索高亮
    function getFontCss(treeId, treeNode) {
        return (!!treeNode.highlight) ? {'color': "#A60000", 'font-weight': "bold"} : {
            'color': "#333",
            'font-weight': "normal"
        };
    }

    // 获取根节点集合
    function getRoots() {
        var treeObj = $.fn.zTree.getZTreeObj(tree_options.id);
        return treeObj.getNodesByFilter(function (node) {
            return node.level == 0;
        });
    }


    _this.getRole = function () {
        var treeObj = $.fn.zTree.getZTreeObj(tree_options.id);
        var rootNode = this.getRoots()[0];
        treeObj.selectNode(rootNode);
        treeObj.setting.callback.onClick(null, tree_options.id, rootNode);
    };
    /*_this.zTree = null;
    _this.rMenu = null;
    // 初始化树
    _this.tree = function(){
        $.ajax({
            url: tree_options.url,
            async: false,
            type: 'post',
            data: {

            },
            success: function(data){
                var zNodes = JSON.parse(data.result);
                // 加载树
                $.fn.zTree.init($("#"+tree_options.id), tree_options.setting, zNodes);
                // 根节点下添加退厂节点
                _this.zTree = $.fn.zTree.getZTreeObj(tree_options.id);
                var rootNode =getRoots()[0];
                var treeNode = new Object();
                treeNode.id = 'quit';
                treeNode.pid = rootNode.id;
                treeNode.name = '离职人员';
                treeNode.companyId = rootNode.id;
                treeNode.departmentId = "-1";
                treeNode.doCheck = false;
                _this.zTree.addNodes(rootNode, treeNode, true);
                setTimeout(function(){
                }, 200);
                _this.zTree.expandAll(true);

                //增加本部节点
               /!* var nodes = _this.zTree.getNodes();
                _this.addYourSelfNode(nodes);*!/
            }
        });
    };*/

    /* _this.addYourSelfNode = function (nodes) {
         if(nodes && nodes.length > 0){
             for(var i = 0; i < nodes.length; i++){
                 var node = nodes[i];
                 if(node && node.isParent){
                     var departmentId = node.departmentId == "" ? "" : node.departmentId;
                     _this.addDefaultNode(node, 0, "yourself_" + node.id, node.id, node.name + "(本部)", node.companyId, departmentId, false);

                     var childs = node.children;
                     _this.addYourSelfNode(childs);
                 }
             }
         }
     };*/

    _this.addDefaultNode = function () {
        var rootNode = getRoots()[0];
        if (rootNode.type != '3') {
            var treeNode = new Object();
            treeNode.id = '-2';
            treeNode.pid = rootNode.id;
            treeNode.name = '调动人员';
            treeNode.companyId = rootNode.id;
            treeNode.departmentId = "-2";
            treeNode.type = '4';
            treeNode.icon = appPath + "/static/global/js/zTree/css/zTreeStyle/img/department.png";
            groupTree.zTree.addNodes(rootNode, treeNode, false);
        }
        var treeNode1 = new Object();
        treeNode1.id = '-1';
        treeNode1.pid = rootNode.id;
        treeNode1.name = '离职人员';
        treeNode1.companyId = rootNode.id;
        treeNode1.departmentId = "-1";
        treeNode1.type = '4';
        treeNode1.icon = appPath + "/static/global/js/zTree/css/zTreeStyle/img/department.png";
        groupTree.zTree.addNodes(rootNode, treeNode1, false);
    };
    /**
     * 得到勾选的单选框
     * @returns {string}
     *
     */
    _this.getCheckedRows_ids = function () {
        // var length = $("#person_table").find("input[type=checkbox]:checked").length;
        // alert(length);
        var userIds = "";
        $("#person_table").find("input[type=checkbox]:checked").each(function () {
            userIds += userIds == '' ? $(this).val() : "," + $(this).val();
        });
        return userIds;
    };

    /**
     * checkbox 点击事件
     * @param selectedNode
     */
    var userIdAll = [];
    var object = {};
    _this.getCheckedRow = function () {
        /**
         * 全选事件
         */
        $("#checkAll").click(function () {
                if (this.checked) {
                    $("#person_table :checkbox").prop("checked", true);

                    $("#person_table :checkbox").each(function () {
                        if (!object[$(this).val()]) {
                            object[$(this).val()] = 1;
                            userIdAll.push($(this).val());
                        }
                    });

                } else {
                    $("#person_table :checkbox").prop("checked", false);
                    if (userIdAll.length > 0) {
                        for (var a = 0; a < userIdAll.length; a++) {
                            $("#person_table :checkbox").each(function () {
                                if ($(this).val() === userIdAll[a]) {
                                    delete object[$(this).val()];
                                    userIdAll.splice(a, 1);
                                }
                            })
                        }
                    }
                }
            }
        );

        /**
         * 单选事件
         */
        $("#person_table").on("click", "input[type=checkbox]", function () {
            if ($(this).is(":checked")) {
                if (!object[$(this).val()]) {
                    object[$(this).val()] = 1;
                    userIdAll.push($(this).val());
                }
            } else {
                if (userIdAll.length > 0) {
                    for (var a = 0; a < userIdAll.length; a++) {
                        if ($(this).val() === userIdAll[a]) {
                            delete object[$(this).val()];
                            userIdAll.splice(a, 1);
                        }
                    }
                }
            }

        });
        /**
         *
         * 清空所有的选择
         */
        $("#clearSelAll").on("click", function () {
            object = {};
            userIdAll = [];
        });
        //console.log(userIdAll);
    }

    _this.checkboxChange = function () {
        if ($("#recursion").prop("checked")) {
            groupTree.init({"ifShow":"true"});
        }else{
            groupTree.init({"ifShow":"false"});
        }
        _this.addDefaultNode();
        _this.search();
    };

    _this.search = function (selectedNode) {
        var selectedNodes = $.fn.zTree.getZTreeObj("group_tree").getSelectedNodes();
        var treeNode = selectedNodes[0];
        // 获取左侧选中节点
        // 获取左侧选中节点
        var companyId = (treeNode.companyId) ? treeNode.companyId : "";
        var departmentId = (treeNode.departmentId) ? treeNode.departmentId : "";
        var departmentName = (treeNode.name) ? treeNode.name : "";
        var type = treeNode.type;
        var id = treeNode.id.toString();
        $("#departmentId").attr("value", departmentId);
        $("#departmentName").attr("value", departmentName);
        var array = [];
        _this.getAllchildrenNodes(array, null, treeNode, type);
        //根节点/单位节点
        if("1" == type || "2" == type){
            $("#companyIds").val(companyId);
            $("#companyId").val(companyId);
            $("#deptIds").val("");
        }else{
            $("#companyIds").val("");
            $("#companyId").val("");
            $("#deptIds").val(array);
        }
        _this.initTable();
    };

//获取某一节点下的所有子节点
    _this.getAllchildrenNodes = function (array, deptNames, treeNode, type) {
        array.push(treeNode.id);
        if (null != deptNames) {
            deptNames.push(treeNode.name);
        }
        if (treeNode.isParent) {
            var childrenNodes = treeNode.children;
            if (childrenNodes) {
                var len = childrenNodes.length;
                if (type == '4') {
                    for (var i = 0; i < len; i++) {
                        _this.getAllchildrenNodes(array, deptNames, childrenNodes[i], type);
                    }
                } else if (type == '3') {
                    for (var m = 0; m < len; m++) {
                        if (childrenNodes[m].type == '4') {
                            continue;
                        }
                        _this.getAllchildrenNodes(array, deptNames, childrenNodes[m], type);
                    }
                } else {
                    for (var j = 0; j < len; j++) {
                        if (childrenNodes[j].type == '3' || childrenNodes[j].type == '4') {
                            continue;
                        }
                        _this.getAllchildrenNodes(array, deptNames, childrenNodes[j], type);
                    }
                }
            }
        }
        return array;
    };

    _this.searchAll = function () {
        $("#person_form").find("input[name=user_name]").val("");
        $("#person_form").find("input[name=role_name]").val("");
        _this.initTable();
    };

    _this.bind_click = function (t) {
        var value = $(t).attr("data-value");
        $("#person_form").find("input[name=role_id]").val(value);
        _this.initTable();
    };
    /**
     * 导出点击事件
     */
    _this.export_info = function () {
        if (page.count != 0) {
            _this.to_export();
        } else {
            layer.msg("没有可以导出的数据！");
        }
    };
    /**
     * 具体的导出方法
     */
    _this.to_export = function () {
        // var getCheckedRows_ids = _this.getCheckedRows_ids();
        if (userIdAll.length === 0) {
            var pageTotal = Number($("#pageTotal").val());
            if (pageTotal && !isNaN(pageTotal)) {
                if (pageTotal > 500) {
                    layer.msg("数据量大于500,请选择小于500条！");
                    return;
                }
            } else {
                return;
            }
            //询问框
            layer.confirm('您要导出当前列表中全部人员？', {
                btn: ['确定', '取消'], //按钮
                icon: 3
            }, function (index) {
                var params = $("#person_form").serialize();
                window.open(appPath + '/admin/dossier/person/export?' + params);
                layer.close(index);
            });
        } else {
            if (userIdAll.length > 500) {
                layer.msg("您的选择大于500条！");
                return;
            }
            // console.log(userIdAll.toString());
            window.open(appPath + '/admin/dossier/person/export?userIds=' + userIdAll.toString());
        }
    };

    _this.ajax = function (url, param, type) {
        var result;
        $.ajax({
            url: url,
            async: false,
            type: 'post',
            data: param,
            success: function (data) {
                result = data;
            }
        });
        return result;
    };
}

var person_dossier = new person_dossier();


